package com.yiwei.hdb.activity;

import android.Manifest;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yiwei.hdb.R;
import com.yiwei.hdb.api.Api;
import com.yiwei.hdb.api.bean.IncomeCarBean;
import com.yiwei.hdb.api.bean.UploadImageBean;
import com.yiwei.hdb.util.GlideRoundTransform;
import com.yiwei.hdb.util.ImageUtil;
import com.yiwei.hdb.util.Network;
import com.yiwei.hdb.util.RxLifeUtil;
import com.yiwei.hdb.util.ToastUtil;
import com.yiwei.hdb.util.UI;
import com.yiwei.hdb.view.V19FrameLayout;
import com.yiwei.hdb.view.uploadImage.UploadImgAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2018/6/10.
 */

public class V3CarIdentityActivity extends BaseActivity {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.img_take_one)
    ImageView imgTakeOne;
    @BindView(R.id.btn_close_one)
    ImageView btnCloseOne;
    @BindView(R.id.layout_take_one)
    FrameLayout layoutTakeOne;
    @BindView(R.id.img_take_two)
    ImageView imgTakeTwo;
    @BindView(R.id.btn_close_two)
    ImageView btnCloseTwo;
    @BindView(R.id.layout_take_two)
    FrameLayout layoutTakeTwo;
    @BindView(R.id.img_take_three)
    ImageView imgTakeThree;
    @BindView(R.id.btn_close_three)
    ImageView btnCloseThree;
    @BindView(R.id.layout_take_three)
    FrameLayout layoutTakeThree;
    @BindView(R.id.img_take_four)
    ImageView imgTakeFour;
    @BindView(R.id.btn_close_four)
    ImageView btnCloseFour;
    @BindView(R.id.layout_take_four)
    FrameLayout layoutTakeFour;
    @BindView(R.id.img_take_five)
    ImageView imgTakeFive;
    @BindView(R.id.btn_close_five)
    ImageView btnCloseFive;
    @BindView(R.id.layout_take_five)
    FrameLayout layoutTakeFive;
    @BindView(R.id.img_take_six)
    ImageView imgTakeSix;
    @BindView(R.id.btn_close_six)
    ImageView btnCloseSix;
    @BindView(R.id.layout_take_six)
    FrameLayout layoutTakeSix;
    @BindView(R.id.img_take_one_sec)
    ImageView imgTakeOneSec;
    @BindView(R.id.btn_close_one_sec)
    ImageView btnCloseOneSec;
    @BindView(R.id.layout_take_one_sec)
    FrameLayout layoutTakeOneSec;
    @BindView(R.id.img_take_two_sec)
    ImageView imgTakeTwoSec;
    @BindView(R.id.btn_close_two_sec)
    ImageView btnCloseTwoSec;
    @BindView(R.id.layout_take_two_sec)
    FrameLayout layoutTakeTwoSec;
    @BindView(R.id.img_take_three_sec)
    ImageView imgTakeThreeSec;
    @BindView(R.id.btn_close_three_sec)
    ImageView btnCloseThreeSec;
    @BindView(R.id.layout_take_three_sec)
    FrameLayout layoutTakeThreeSec;
    @BindView(R.id.img_take_four_sec)
    ImageView imgTakeFourSec;
    @BindView(R.id.btn_close_four_sec)
    ImageView btnCloseFourSec;
    @BindView(R.id.layout_take_four_sec)
    FrameLayout layoutTakeFourSec;
    @BindView(R.id.img_take_five_sec)
    ImageView imgTakeFiveSec;
    @BindView(R.id.btn_close_five_sec)
    ImageView btnCloseFiveSec;
    @BindView(R.id.layout_take_five_sec)
    FrameLayout layoutTakeFiveSec;
    @BindView(R.id.img_take_six_sec)
    ImageView imgTakeSixSec;
    @BindView(R.id.btn_close_six_sec)
    ImageView btnCloseSixSec;
    @BindView(R.id.layout_take_six_sec)
    FrameLayout layoutTakeSixSec;
    @BindView(R.id.btn_submit)
    TextView btnSubmit;
    @BindView(R.id.scrollView1)
    HorizontalScrollView scrollView1;
    @BindView(R.id.scrollView2)
    HorizontalScrollView scrollView2;
    private Unbinder unbinder;
    private UploadImgAdapter uploadImgAdapter;
    private int take_type;
    private boolean is_one_ok;
    private boolean is_one_sec_ok;
    private String one_url;
    private String two_url;
    private String three_url;
    private String four_url;
    private String five_url;
    private String six_url;

    private String one_sec_url;
    private String two_sec_url;
    private String three_sec_url;
    private String four_sec_url;
    private String five_sec_url;
    private String six_sec_url;

    private List<String> car_image = new ArrayList<>();
    private List<String> identity_image = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_car_identity_v3);
        initBar();
        unbinder = ButterKnife.bind(this);
        init();
    }

    private void init() {
        txtTitle.setText("车辆认证");
    }

    private void goToTakePhoto() {
        checkPermission(new CheckPermListener() {
            @Override
            public void superPermission() {
                if (uploadImgAdapter == null) {
                    initUploadImgAdapter();
                }
                uploadImgAdapter.withMode(UploadImgAdapter.MODE_IMG).showDialog();
            }

            @Override
            public void superReject() {

            }
        }, R.string.camera_storage_card_p, R.string.get_cammra, Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE);
    }

    private void initUploadImgAdapter() {
        uploadImgAdapter = new UploadImgAdapter(this, new UploadImgAdapter.Action2() {
            @Override
            public void call(Bitmap file, int mode) {
                if (mode == UploadImgAdapter.MODE_IMG) {
                    uploadImg(file);
                }
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (uploadImgAdapter != null) {
            uploadImgAdapter.onActivityResult(requestCode, resultCode, data);
        }

    }

    private void getIncomeCarV3(List<String> car_image, List<String> identity_image) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.makeText(this, "网络连接失败");
            return;
        }
        loadingDialog.show();
        IncomeCarBean bean = new IncomeCarBean();
        bean.car_image = car_image;
        bean.identity_image = identity_image;
        Api.api_service.getIncomeCarV3(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    ToastUtil.makeText(this, "上传成功");
                    finish();
                });
    }


    private void uploadImg(Bitmap img) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.makeText(this, "网络连接失败");
            return;
        }
        loadingDialog.show();

        UploadImageBean bean = new UploadImageBean();
        bean.img = ImageUtil.Bitmap2StrByBase64(img, 100);
        Api.api_service.uploadImg(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    if (take_type == 0) {
                        one_url = result.getUrl();
                        Glide.with(this).load(result.getUrl()).transform(new GlideRoundTransform(this, 10)).into(imgTakeOne);
                        is_one_ok = true;
                        btnCloseOne.setVisibility(View.VISIBLE);
                        layoutTakeTwo.setVisibility(View.VISIBLE);

                    } else if (take_type == 1) {
                        two_url = result.getUrl();
                        Glide.with(this).load(result.getUrl()).transform(new GlideRoundTransform(this, 10)).into(imgTakeTwo);
                        btnCloseTwo.setVisibility(View.VISIBLE);
                        layoutTakeThree.setVisibility(View.VISIBLE);
                    } else if (take_type == 2) {
                        three_url = result.getUrl();
                        Glide.with(this).load(result.getUrl()).transform(new GlideRoundTransform(this, 10)).into(imgTakeThree);
                        btnCloseThree.setVisibility(View.VISIBLE);
                        layoutTakeFour.setVisibility(View.VISIBLE);
                        scrollView1.post(new Runnable() {
                            @Override
                            public void run() {
                                scrollView1.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
                            }
                        });

                    } else if (take_type == 3) {
                        four_url = result.getUrl();
                        Glide.with(this).load(result.getUrl()).transform(new GlideRoundTransform(this, 10)).into(imgTakeFour);
                        btnCloseFour.setVisibility(View.VISIBLE);
                        layoutTakeFive.setVisibility(View.VISIBLE);
                        scrollView1.post(new Runnable() {
                            @Override
                            public void run() {
                                scrollView1.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
                            }
                        });
                    } else if (take_type == 4) {
                        five_url = result.getUrl();
                        Glide.with(this).load(result.getUrl()).transform(new GlideRoundTransform(this, 10)).into(imgTakeFive);
                        btnCloseFive.setVisibility(View.VISIBLE);
                        layoutTakeSix.setVisibility(View.VISIBLE);
                        scrollView1.post(new Runnable() {
                            @Override
                            public void run() {
                                scrollView1.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
                            }
                        });
                    } else if (take_type == 5) {
                        six_url = result.getUrl();
                        Glide.with(this).load(result.getUrl()).transform(new GlideRoundTransform(this, 10)).into(imgTakeSix);
                        btnCloseSix.setVisibility(View.VISIBLE);
                    } else if (take_type == 6) {
                        one_sec_url = result.getUrl();
                        Glide.with(this).load(result.getUrl()).transform(new GlideRoundTransform(this, 10)).into(imgTakeOneSec);
                        is_one_sec_ok = true;
                        layoutTakeTwoSec.setVisibility(View.VISIBLE);
                        btnCloseOneSec.setVisibility(View.VISIBLE);
                    } else if (take_type == 7) {
                        two_sec_url = result.getUrl();
                        Glide.with(this).load(result.getUrl()).transform(new GlideRoundTransform(this, 10)).into(imgTakeTwoSec);
                        layoutTakeThreeSec.setVisibility(View.VISIBLE);
                        btnCloseTwoSec.setVisibility(View.VISIBLE);
                    } else if (take_type == 8) {
                        three_sec_url = result.getUrl();
                        Glide.with(this).load(result.getUrl()).transform(new GlideRoundTransform(this, 10)).into(imgTakeThreeSec);
                        layoutTakeFour.setVisibility(View.VISIBLE);
                        btnCloseThreeSec.setVisibility(View.VISIBLE);
                        scrollView2.post(new Runnable() {
                            @Override
                            public void run() {
                                scrollView2.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
                            }
                        });
                    } else if (take_type == 9) {
                        four_sec_url = result.getUrl();
                        Glide.with(this).load(result.getUrl()).transform(new GlideRoundTransform(this, 10)).into(imgTakeFourSec);
                        layoutTakeFive.setVisibility(View.VISIBLE);
                        btnCloseFourSec.setVisibility(View.VISIBLE);
                        scrollView2.post(new Runnable() {
                            @Override
                            public void run() {
                                scrollView2.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
                            }
                        });
                    } else if (take_type == 10) {
                        five_sec_url = result.getUrl();
                        Glide.with(this).load(result.getUrl()).transform(new GlideRoundTransform(this, 10)).into(imgTakeFiveSec);
                        layoutTakeSix.setVisibility(View.VISIBLE);
                        btnCloseFiveSec.setVisibility(View.VISIBLE);
                        scrollView2.post(new Runnable() {
                            @Override
                            public void run() {
                                scrollView2.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
                            }
                        });
                    } else if (take_type == 11) {
                        six_sec_url = result.getUrl();
                        Glide.with(this).load(result.getUrl()).transform(new GlideRoundTransform(this, 10)).into(imgTakeSixSec);
                        btnCloseSixSec.setVisibility(View.VISIBLE);
                    }

                    if (is_one_sec_ok && is_one_ok) {
                        btnSubmit.setSelected(true);
                    } else {
                        btnSubmit.setSelected(false);
                    }
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
        unbinder = null;
    }

    @OnClick({R.id.btn_back, R.id.img_take_one, R.id.btn_close_one, R.id.img_take_two, R.id.btn_close_two, R.id.img_take_three, R.id.btn_close_three, R.id.img_take_four, R.id.btn_close_four, R.id.img_take_five, R.id.btn_close_five, R.id.img_take_six, R.id.btn_close_six, R.id.img_take_one_sec, R.id.btn_close_one_sec, R.id.img_take_two_sec, R.id.btn_close_two_sec, R.id.img_take_three_sec, R.id.btn_close_three_sec, R.id.img_take_four_sec, R.id.btn_close_four_sec, R.id.img_take_five_sec, R.id.btn_close_five_sec, R.id.img_take_six_sec, R.id.btn_close_six_sec, R.id.btn_submit})
    public void onViewClicked(View view) {
        if (UI.isFastClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.img_take_one:
                take_type = 0;
                goToTakePhoto();
                break;
            case R.id.btn_close_one:
                imgTakeOne.setImageResource(R.mipmap.img_v3_car_identity_take_);
                btnCloseOne.setVisibility(View.GONE);
                one_url = "";
                break;
            case R.id.img_take_two:
                take_type = 1;
                goToTakePhoto();
                break;
            case R.id.btn_close_two:
                imgTakeTwo.setImageResource(R.mipmap.img_v3_car_identity_take_);
                btnCloseTwo.setVisibility(View.GONE);
                two_url = "";
                break;
            case R.id.img_take_three:
                take_type = 2;
                goToTakePhoto();
                break;
            case R.id.btn_close_three:
                imgTakeThree.setImageResource(R.mipmap.img_v3_car_identity_take_);
                btnCloseThree.setVisibility(View.GONE);
                three_url = "";
                break;
            case R.id.img_take_four:
                take_type = 3;
                goToTakePhoto();
                break;
            case R.id.btn_close_four:
                imgTakeFour.setImageResource(R.mipmap.img_v3_car_identity_take_);
                btnCloseFour.setVisibility(View.GONE);
                four_url = "";
                break;
            case R.id.img_take_five:
                take_type = 4;
                goToTakePhoto();
                break;
            case R.id.btn_close_five:
                imgTakeFive.setImageResource(R.mipmap.img_v3_car_identity_take_);
                btnCloseFive.setVisibility(View.GONE);
                five_url = "";
                break;
            case R.id.img_take_six:
                take_type = 5;
                goToTakePhoto();
                break;
            case R.id.btn_close_six:
                imgTakeSix.setImageResource(R.mipmap.img_v3_car_identity_take_);
                btnCloseSix.setVisibility(View.GONE);
                six_url = "";
                break;
            case R.id.img_take_one_sec:
                take_type = 6;
                goToTakePhoto();
                break;
            case R.id.btn_close_one_sec:
                imgTakeOneSec.setImageResource(R.mipmap.img_v3_car_identity_take_);
                btnCloseOneSec.setVisibility(View.GONE);
                one_sec_url = "";
                break;
            case R.id.img_take_two_sec:
                take_type = 7;
                goToTakePhoto();
                break;
            case R.id.btn_close_two_sec:
                imgTakeTwoSec.setImageResource(R.mipmap.img_v3_car_identity_take_);
                btnCloseTwoSec.setVisibility(View.GONE);
                two_sec_url = "";
                break;
            case R.id.img_take_three_sec:
                take_type = 8;
                goToTakePhoto();
                break;
            case R.id.btn_close_three_sec:
                imgTakeThreeSec.setImageResource(R.mipmap.img_v3_car_identity_take_);
                btnCloseThreeSec.setVisibility(View.GONE);
                three_sec_url = "";
                break;
            case R.id.img_take_four_sec:
                take_type = 9;
                goToTakePhoto();
                break;
            case R.id.btn_close_four_sec:
                imgTakeFourSec.setImageResource(R.mipmap.img_v3_car_identity_take_);
                btnCloseFourSec.setVisibility(View.GONE);
                four_sec_url = "";
                break;
            case R.id.img_take_five_sec:
                take_type = 10;
                goToTakePhoto();
                break;
            case R.id.btn_close_five_sec:
                imgTakeFiveSec.setImageResource(R.mipmap.img_v3_car_identity_take_);
                btnCloseFiveSec.setVisibility(View.GONE);
                five_sec_url = "";
                break;
            case R.id.img_take_six_sec:
                take_type = 11;
                goToTakePhoto();
                break;
            case R.id.btn_close_six_sec:
                imgTakeSixSec.setImageResource(R.mipmap.img_v3_car_identity_take_);
                btnCloseSixSec.setVisibility(View.GONE);
                six_sec_url = "";
                break;
            case R.id.btn_submit:
                if ((!TextUtils.isEmpty(one_url)
                        || !TextUtils.isEmpty(two_url)
                        || !TextUtils.isEmpty(three_url)
                        || !TextUtils.isEmpty(four_url)
                        || !TextUtils.isEmpty(five_url)
                        || !TextUtils.isEmpty(six_url))
                        && (!TextUtils.isEmpty(one_sec_url)
                        || !TextUtils.isEmpty(two_sec_url)
                        || !TextUtils.isEmpty(three_sec_url)
                        || !TextUtils.isEmpty(four_sec_url)
                        || !TextUtils.isEmpty(five_sec_url))
                        || !TextUtils.isEmpty(six_sec_url)) {
                    car_image.clear();
                    if (!TextUtils.isEmpty(one_url)) {
                        car_image.add(one_url);
                    }
                    if (!TextUtils.isEmpty(two_url)) {
                        car_image.add(two_url);
                    }
                    if (!TextUtils.isEmpty(three_url)) {
                        car_image.add(three_url);
                    }
                    if (!TextUtils.isEmpty(four_url)) {
                        car_image.add(four_url);
                    }
                    if (!TextUtils.isEmpty(five_url)) {
                        car_image.add(five_url);
                    }
                    if (!TextUtils.isEmpty(six_url)) {
                        car_image.add(six_url);
                    }

                    identity_image.clear();
                    if (!TextUtils.isEmpty(one_sec_url)) {
                        identity_image.add(one_sec_url);
                    }
                    if (!TextUtils.isEmpty(two_sec_url)) {
                        identity_image.add(two_sec_url);
                    }
                    if (!TextUtils.isEmpty(three_sec_url)) {
                        identity_image.add(three_sec_url);
                    }
                    if (!TextUtils.isEmpty(four_sec_url)) {
                        identity_image.add(four_sec_url);
                    }
                    if (!TextUtils.isEmpty(five_sec_url)) {
                        identity_image.add(five_sec_url);
                    }
                    if (!TextUtils.isEmpty(six_sec_url)) {
                        identity_image.add(six_sec_url);
                    }
                    getIncomeCarV3(car_image, identity_image);
                } else {
                    ToastUtil.makeText(this, "请上传相关图片");
                }
                break;
        }
    }
}
